package com.example.log.myaop;

import com.example.log.entity.Log;
import com.example.log.service.ILogService;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Aspect
@Component
public class MyUserLog {


    private final ILogService logService;

    public MyUserLog(ILogService logService) {
        this.logService = logService;
    }

    @Pointcut("@annotation(com.example.log.myaop.UserLog)")
    public void MyPointCut() {
    }


    @After("MyPointCut()")
    public void after(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Object[] args = joinPoint.getArgs();
        for (Object o : args) {
            System.out.println("argus=" + o);
        }
        UserLog annotation = signature.getMethod().getAnnotation(UserLog.class);
        OperationType type = annotation.type();
        Log log = new Log();
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        assert requestAttributes != null;
        HttpServletRequest request = requestAttributes.getRequest();
        String ipAddress = getIpAddress(request);
        System.out.println("-------------------------ip=" + ipAddress);
        saveLog(type, log);
    }


    public void saveLog(OperationType type, Log log) {
        String value = type.getValue();
        switch (value) {
            case "insert":
                log.setContext("插入了一个新用户");
                break;
            case "delete":
                log.setContext("删除一个用户");
                break;
            case "update":
                log.setContext("跟新一个用户");
                break;
            case "select":
                log.setContext("查询一个用户");
                break;
        }
        logService.save(log);
    }


    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
